<?php

session_start();
/*
 * duyet cac anh trong album de lay ve buc anh co so luong like cao nhat
 */
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( './FacebookAction/FacebookAlbum.php' );
require_once( './FacebookAction/FacebookAccesstoken.php' );
require_once( './FacebookAction/FacebookPhoto.php' );
require_once( './FacebookAction/FacebookLike.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;

error_reporting (E_ALL ^ E_NOTICE);//xoa warning notice treb man hinh
include 'config.php';
FacebookSession::setDefaultApplication($id, $secret);

$helper = new FacebookRedirectLoginHelper('http://localhost/itv-facebook-app/requestlikes.php');

try {
    $session = $helper->getSessionFromRedirect();
} catch (Exception $e) {
    
}

if (isset($_SESSION['token'])) {

    $session = new FacebookSession($_SESSION['token']);

    try {

        $session->Validate($id, $secret);
    } catch (FacebookAuthorizationException $e) {

        $session = '';
    }
}

$fl = new \FacebookAction\FacebookLike();
$fp = new \FacebookAction\FacebookPhoto();

if (isset($session)) {
    set_time_limit(0);
    $json_album = json_decode(file_get_contents('album_id.json'),true);
    $json_return = $fp->getAllPhotoFromAlbum($session, $json_album['id']);
    $start = true;
    while ($start) {
            //--------Khoi tao bien tham chieu de nhan gia tri lon nhat
            $json_temp = $fl->getLikeOfObject($session, $json_return['data'][0]['id']);            
            $id = $json_return['data'][0]['id'];//id cua photo dau tien
            $height = count($json_temp['data']);//so like cua photo dau tien
            $count_json = count($json_return['data']);//so phan photo
            for($i=0; $i<$count_json; $i++){
                $json_temp1 = $fl->getLikeOfObject($session, $json_return['data'][$i]['id']); 
                $height1 = count($json_temp1['data']);//so like cua photo dang duyet
                if($height < $height1){
                    $height = $height1;
                    $id = $json_return['data'][$i]['id'];
                }
            }
        echo '<br>id: ' . $id;
        echo '<br>likes: ' . $height;
        echo '<br>------------------end------------------';
        flush();
        ob_flush();
        sleep(3);
        //------------ dieu kien de dung vong ----------------
    }
} else {
    echo "<a href = " . $helper->getLoginUrl() . ">Login With Facebook</a>";
}










